#include <iostream>
#include <vector>
using namespace std;

// Fragment 结构体
struct Fragment {
    int length;
    int offset;
};

vector<Fragment> fragmentPacket(int packetLength, const vector<int>& pathMTUs) {
    vector<Fragment> fragments;
    int dataLength = packetLength - 20; 
    int currentOffset = 0;

    for (int mtu : pathMTUs) {
        int maxDataLength = mtu - 20;
        if (maxDataLength < 8) {
            cerr << "MTU 过小，无法进行分片。" << endl;
            return {};
        }

        vector<Fragment> tempFragments;
        int remainingData = dataLength;
        int currentOffsetTemp = currentOffset;

        while (remainingData > 0) {
            int fragmentDataLength = (remainingData > maxDataLength) ? maxDataLength : remainingData;
            // 偏移量是 8 的倍数
            int alignedFragmentDataLength = (fragmentDataLength / 8) * 8;
            Fragment fragment;
            fragment.length = alignedFragmentDataLength + 20;
            fragment.offset = currentOffsetTemp;
            tempFragments.push_back(fragment);

            remainingData -= alignedFragmentDataLength;
            currentOffsetTemp += alignedFragmentDataLength / 8;
        }

        // 更新当前偏移量和数据长度
        if (!tempFragments.empty()) {
            currentOffset = tempFragments.back().offset + tempFragments.back().length / 8;
            dataLength = remainingData;
            fragments = tempFragments;
        }
    }

    return fragments;
}    